package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.RspdTimer;
import com.bestcem.xm.ticket.entity.mysql.RspdTimerDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * DO和DTO转换类
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/17 16:10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RspdTimerConverter {

    @Nullable
    public static RspdTimer doToDto(RspdTimerDO rspdTimerDO) {
        if (Objects.isNull(rspdTimerDO)) {
            return null;
        }

        RspdTimer rspdTimer = new RspdTimer();
        rspdTimer.setId(rspdTimerDO.getRspdTimerId());
        rspdTimer.setExpire(rspdTimerDO.getExpire());
        rspdTimer.setRuleIdList(CommonConverter.stringToList(rspdTimerDO.getRuleIdList(), String.class));
        if (StringUtils.isNotEmpty(rspdTimerDO.getAgainDeliverRecordId())) {
            RspdTimer.Search search = new RspdTimer.Search();
            search.setAgainDeliverRecordId(rspdTimerDO.getAgainDeliverRecordId());
            rspdTimer.setSearch(search);
        }

        CommonConverter.setEntity(rspdTimer, rspdTimerDO);
        return rspdTimer;
    }

    @Nonnull
    public static List<RspdTimer> doListToDtoList(List<RspdTimerDO> rspdTimerDOList) {
        List<RspdTimer> list = new ArrayList<>();
        if (Objects.isNull(rspdTimerDOList)) {
            return list;
        }

        for (RspdTimerDO rspdTimerDO : rspdTimerDOList) {
            RspdTimer rspdTimer = doToDto(rspdTimerDO);
            list.add(rspdTimer);
        }
        return list;
    }

    @Nullable
    public static RspdTimerDO dtoToDo(RspdTimer rspdTimer) {
        if (Objects.isNull(rspdTimer)) {
            return null;
        }

        RspdTimerDO rspdTimerDO = new RspdTimerDO();
        rspdTimerDO.setRspdTimerId(rspdTimer.getId());
        rspdTimerDO.setExpire(rspdTimer.getExpire());
        rspdTimerDO.setRuleIdList(CommonConverter.objectToString(rspdTimer.getRuleIdList()));
        if (Objects.nonNull(rspdTimer.getSearch())) {
            rspdTimerDO.setAgainDeliverRecordId(rspdTimer.getSearch().getAgainDeliverRecordId());
        }

        CommonConverter.setDo(rspdTimer, rspdTimerDO);
        return rspdTimerDO;
    }
}
